package com.myblog.controller;



import com.myblog.entity.BlogMessage;
import com.myblog.service.impl.BlogMessageServiceImpl;
import com.myblog.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhj
 * @since 2022-05-25
 */
@Controller
@RequestMapping("/blogMessage")
public class BlogMessageController {

    @Autowired
    private BlogMessageServiceImpl blogMessageService;

    @RequestMapping("/getBlogMessage")
    @ResponseBody
    public PageResult getBlogMessage(@RequestParam(value = "pageSize") Integer pageSize,
                                     @RequestParam(value = "pageNum") Integer pageNum){
        PageResult page = blogMessageService.findPage(pageSize, pageNum);
        return page;
    }

    @RequestMapping("/addMessage")
    public String addMessage(BlogMessage blogMessage, RedirectAttributes redirectAttributes){
        int i = blogMessageService.addMessage(blogMessage);
        String msg;
        if (i == 0 || !"".equals(i)){
            msg = "发表成功！";
        }else {
            msg = "系统出错，稍后再试";
        }
        redirectAttributes.addFlashAttribute("msg", msg);
        return "redirect:/guestBook";
    }


}
